// hyb_yl/mysubpages/pages/physicalOrder/physicalOrder.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: [{
        name: "全部"
      },
      {
        name: "待支付"
      },
      {
        name: "待体检"
      },
      {
        name: "已完成"
      },
      {
        name: "已取消"
      },
    ], //选项卡
    current: 0,
    index: 0,
    unscramble: [],
    wzf: [],
    ySuccess: [],
    tijians: [],
    concals: [],
  },
  // 切换
  chooseTab: function (e) {
    var index = e.currentTarget.dataset.i
    this.setData({
      current: index,
    })
    var unscramble = this.data.unscramble
    var wzf = []
    var ySuccess = []
    let tijians = []
    let concals = []
    // if (index == 1) {
    //   for (let i = 0; i < unscramble.length; i++) {
    //     if (unscramble[i].ifpay == 0) {
    //       wzf.push(unscramble[i])
    //     }
    //   }
    //   this.setData({
    //     wzf: wzf
    //   })
    // } else if (index == 2) {
    //   for (let i = 0; i < unscramble.length; i++) {
    //     if (unscramble[i].ifover == 0 && unscramble[i].ifpay == 1) {
    //       ySuccess.push(unscramble[i])
    //     }
    //   }
    //   this.setData({
    //     ySuccess: ySuccess
    //   })
    // } else if (index == 3) {
    //   for (let i = 0; i < unscramble.length; i++) {
    //     if (unscramble[i].ifover == 1) {
    //       tijians.push(unscramble[i])
    //     }
    //   }
    //   this.setData({
    //     tijians
    //   })
    // } else if (index == 4) {
    //   for (let i = 0; i < unscramble.length; i++) {
    //     if (unscramble[i].ifgb == 1 && unscramble[i].ifpay == 1) {
    //       concals.push(unscramble[i])
    //     }
    //   }
    //   this.setData({
    //     concals
    //   })
    // }
    this.setData({
      index: index
    })
    this.getMytjianorder(index)
  },
  // orderinfo:function(e){
  //   // var unscramble = this.data.unscramble
  //   // console.log(unscramble)
  //   console.log(e)
  //   var id= e.currentTarget.dataset.id
  //   wx.navigateTo({
  //     url: '/hyb_yl/userLife/pages/tijian/detail/detail?tid=' +id,
  //   })
  // },
  lookdetail: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    //将每个检查项目生成二维码
    var overtime = e.currentTarget.dataset.overtime
    app.util.request({
      'url': 'entry/wxapp/tijian.oderinfo',
      data: {
        id: id
      },
      success: function (res) {
        console.log(res)
        wx.navigateTo({
          url: '/hyb_yl/mysubpages/pages/phyDetail/phyDetail?id=' + id + '&overtime=' + overtime,
        })
      }
    })

  },
  tixing: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/diagReport/diagReport?id=' + id + '&ifzj=0',
    })
  },
  zhifu(e) {
    var that = this
    var money = e.currentTarget.dataset.money
    var orders = e.currentTarget.dataset.order
    app.util.request({
      'url': 'entry/wxapp/tijian.paytijianorder',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        z_tw_money: money,
        orders: orders
      },
      success: function (res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {
            //跳转订单页
            wx.showToast({
              title: '支付成功',
              icon: 'none',
              duration: 1500,
              success: function (res) {
                wx.redirectTo({
                  url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                })
              }
            })
          },
          fail: function () {
            //支付失败
            wx.redirectTo({
              url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMytjianorder(0)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getMytjianorder(index) {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/tijian.orderlist',
      data: {
        openid: wx.getStorageSync('openid'),
        index: index
      },
      success: function (res) {
       // console.log(res)
        that.setData({
          unscramble: res.data
        })
      }
    })
  },
  delete(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var index = that.data.index
    wx.showModal({
      title: '提示',
      content: '操作后订单不可恢复是否继续',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/tijian.deleteorder',
            data: {
              id: id
            },
            success: function (res) {
              console.log(res)
              that.getMytjianorder(index)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  quxiao(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var index = that.data.index
    wx.showModal({
      title: '提示',
      content: '操作后订单不可恢复是否继续',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/tijian.updateqorder',
            data: {
              id: id
            },
            success: function (res) {
              console.log(res)
              that.getMytjianorder(index)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  }
})